package ran.friend.controller.user;

import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ran.constants.HttpConstants;
import ran.controller.BaseController;
import ran.friend.domain.user.dto.UserCodeDTO;
import ran.entity.Result;
import ran.friend.domain.user.dto.UserEditDTO;
import ran.friend.domain.user.vo.UserVO;
import ran.friend.service.user.IUserService;
import ran.vo.LoginUserVO;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-11-03
 * Time: 10:49
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserCodeDTO codeDTO) throws MessagingException, UnsupportedEncodingException {
        return toResult(userService.sendCode(codeDTO));
    }
    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserCodeDTO codeDTO) {
        return Result.ok(userService.codeLogin(codeDTO.getMail(),codeDTO.getCode()));
    }
    // 退出登录
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toResult(userService.logout(token));
    }
    // 获取用户昵称和头像信息
    // 从 Header 中的 Authorization 中取出 token
    @RequestMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    // 获取用户详情信息, 因为我们已经将userId存储到ThreadLocal中, 所以在拦截器已经新型了token解析, 这里不需要再接收token
    @GetMapping ("/detail")
    public Result<UserVO> detail() {
        return Result.ok(userService.detail());
    }

    // 修改用户信息
    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserEditDTO userEditDTO) {
        return toResult(userService.edit(userEditDTO));
    }

    // 更新头像
    @PutMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestBody UserEditDTO userEditDTO) {
        return toResult(userService.updateHeadImage(userEditDTO.getHeadImage()));
    }
}
